#help.py

import tkinter as tk
from tkhtmlview import HTMLLabel
import markdown2

class HelpWindow:
    def __init__(self, language='Chinese'):
        self.language = language

        self.window = tk.Toplevel()
        self.window.title("帮助 / Help")
        self.window.geometry("800x600")

        self.language_button = tk.Button(self.window, text="中文/English", command=self.toggle_language)
        self.language_button.pack()

        self.html_content_chinese = self.convert_markdown_to_html("README.md")
        self.html_content_english = self.convert_markdown_to_html("README.en.md")

        self.html_view = HTMLLabel(self.window, html=self.html_content_chinese)
        self.html_view.pack(expand=True, fill="both")

    def toggle_language(self):
        if self.language == 'Chinese':
            self.language = 'English'
            self.html_view.set_html(self.html_content_english)
        else:
            self.language = 'Chinese'
            self.html_view.set_html(self.html_content_chinese)

    def convert_markdown_to_html(self, filename):
        try:
            with open(filename, "r", encoding="utf-8") as f:
                markdown_content = f.read()
                # 使用 markdown 库将 Markdown 内容转换为 HTML 格式
                html_content = markdown2.markdown(markdown_content)
                                # 删除<code>标签，但保留其内容
                html_content = html_content.replace('<code>', '').replace('</code>', '')   
                # 在 HTML 内容中为每个标签添加 CSS 样式
                tags_to_replace = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'ol', 'ul', 'li', 'a']
                # 针对每个需要替换的标签，使用字符串替换方法将其替换为带有楷体字体样式的标签
                for tag in tags_to_replace:
                    html_content = html_content.replace(f"<{tag}>", f"<{tag} style=\"font-family: 微软雅黑, sans-serif;\">")
                return html_content
        except FileNotFoundError:
            return "<h1>File not found</h1>"

    def run(self):
        self.window.mainloop()

if __name__ == "__main__":
    root = tk.Tk()
    help_window = HelpWindow()
    help_window.run()
